<template>
  <el-dialog
    title="新增员工"
    :visible="showDialog"
    width="40%"
    center
    @close="closeDialog"
  >
    <!-- 主体表单内容 -->
    <el-form ref="addFormRef" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width:50%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width:50%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" style="width:50%" placeholder="请选择日期" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width:50%" placeholder="请选择">
          <el-option v-for="item in EmployeeEnum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width:50%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- <el-input v-model="formData.departmentName" style="width:50%" placeholder="请选择部门" @focus="getDepartments" /> -->
        <!-- 树形列表 -->
        <!-- <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :props="{ label: 'name' }"
        /> -->
        <!-- 级联选择器 -->
        <el-cascader
          v-model="formData.departmentName"
          :options="options"
          :props="propOpt"
          placeholder="请选择部门"
          style="width:80%"
          @focus="getDepartments"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" style="width:50%" placeholder="请选择日期" />
      </el-form-item>
    </el-form>
    <!-- 按钮操作区域 -->
    <span slot="footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="addFormOk">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getDepartments } from '@/api/departments'
import { addEmployee } from '@/api/employees'
import { tranListToTreeData } from '@/utils'
// 聘用状态文件引用
import EmployeeEnum from '@/api/constant/employees'
export default {

  props: {
    // 对话框显示的标识
    showDialog: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      // 聘用状态数据
      EmployeeEnum,
      // 表单数据
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      // 表单校验规则
      rules: {
        username: [{ required: true, message: '用户姓名不能为空', trigger: 'blur' }, {
          min: 1, max: 4, message: '用户姓名为1-4位'
        }],
        mobile: [{ required: true, message: '手机号不能为空', trigger: 'blur' }, {
          pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur'
        }],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [{ required: true, message: '工号不能为空', trigger: 'blur' }],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'blur' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      },

      // 树形结构列表数据
      treeData: [],

      // 树形结构的显示标识
      showTree: false,

      // 加载图标显示标识
      loading: false,

      // 级联选择器当前选中的部门
      // value: [],

      // 级联选择器数据
      options: [],

      // 级联选择器配置对象
      propOpt: {
        expandTrigger: 'hover',
        emitPath: false,
        value: 'name',
        label: 'name'
      }
    }
  },

  methods: {

    // 打开级联选择器
    async getDepartments() {
      // this.loading = true
      // this.showTree = true
      // 请求组织架构方法
      const { depts } = await getDepartments()
      // 转化为树形结构数组
      this.options = tranListToTreeData(depts, '')
      // this.options = otherTranListToTreeData(depts)
      // this.loading = false
    },

    // 确认添加员工的方法
    async addFormOk() {
      // 表单整体检验
      const res = await this.$refs.addFormRef.validate().catch(err => err)
      // 点击确认时提交表单信息给后台
      if (res) {
        await addEmployee(this.formData)
        this.$message({
          message: '成功添加员工',
          type: 'success'
        })
        //  利用$parent获取父组件实例
        // this.$parent.getEmployeeLsit && this.$parent.getEmployeeList()
        this.$emit('update')
        this.$parent.showDialog = false
      }
    },

    // 关闭对话框方法
    closeDialog() {
      this.formData = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
      this.$refs.addFormRef.resetFields()
      this.$emit('update:showDialog', false)
    }

  }
}
</script>

<style>

</style>
